package com.skt.common_hr.mintview.view
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.components.CommonAlert;
	import com.skt.common_hr.common.puremvc.CommonMediator;
	import com.skt.common_hr.mintview.MintViewFacade;
	import com.skt.common_hr.mintview.model.NoticeWriteProxy;
	import com.skt.common_hr.mintview.model.vo.NoticeDetailVO;
	import com.skt.common_hr.mintview.model.vo.NoticeFileUpLoadVO;
	import com.skt.common_hr.mintview.model.vo.NoticeVO;
	import com.skt.common_hr.mintview.model.vo.ProfileVO;
	import com.skt.common_hr.mintview.model.vo.mintListVO;
	import com.skt.common_hr.mintview.view.components.NoticeWrite;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class NoticeWriteMediator extends CommonMediator
	{
		public static const NAME:String = "NoticeWriteMediator";
		public static const CLOSED_REQUEST_VIEW:String	= "closedRequestView";
		
		private const MODE_WAITING:String = "modeWaiting";
		private const MODE_WRITING:String = "modeWriting";
		private const MODE_MODIFY:String = "modeModify";
		
		private var _mode:String = MODE_WAITING;
		private var _bWaitingSave:Boolean = false;
		private var orgFileName:String = "";
		
		public function NoticeWriteMediator(viewCompo:Object)
        {
            super( NAME, viewCompo );
        }
		
		override protected function initializeView():void
		{
			super.initializeView();
			setEventListener();
		}
		
		private function setEventListener():void
		{
			view.addEventListener(NoticeWrite.NoticeSave, onNoticeSave);
			view.addEventListener(NoticeWrite.NoticeDelete, onNoticeDelete);
			view.addEventListener(NoticeWrite.NoticeCancel, onCancel);
		}
		
		private function removeEventListener():void
		{
			view.removeEventListener(NoticeWrite.NoticeSave, onNoticeSave);
			view.removeEventListener(NoticeWrite.NoticeDelete, onNoticeDelete);
			view.removeEventListener(NoticeWrite.NoticeCancel, onCancel);
		}
		
		override protected function clearView():void
		{
			view.dataProvider = null;
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					MintViewFacade.WRITE_SEARCH_NOTICE,
					MintViewFacade.WRITE_SEARCH_NOTICE_COMPLETE,
					MintViewFacade.MODE_WRITE_NOTICE,
					MintViewFacade.MODE_MODIFY_NOTICE,
					MintViewFacade.INSERT_NOTICE_COMPLETE,
					MintViewFacade.INSERT_NOTICE_FAILED,
					MintViewFacade.UPDATE_NOTICE_COMPLETE,
					MintViewFacade.UPDATE_NOTICE_FAILED,
					];
		}
		
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case MintViewFacade.WRITE_SEARCH_NOTICE:
					loadWriteData(note.getBody() as mintListVO);
					break;
				case MintViewFacade.WRITE_SEARCH_NOTICE_COMPLETE:
					view.dataProvider = (note.getBody() as NoticeWriteProxy).noticeListArr;
					view.fileListArr = new ArrayCollection();
					view.fileListArr = (note.getBody() as NoticeWriteProxy).noticeFileArr;
					if(view.fileListArr.length != 0){
						view.fileCompletLen = view.fileListArr.length;	
					}
					 
					break;	
				case MintViewFacade.MODE_WRITE_NOTICE:
//					startWriting();
					loadNewWriteData(note.getBody() as ProfileVO);
					break;
				case MintViewFacade.MODE_MODIFY_NOTICE:
					break;
				case MintViewFacade.INSERT_NOTICE_COMPLETE:
					stopWriting();
					break;
				case MintViewFacade.INSERT_NOTICE_FAILED:
//					CommonAlert.showMessage(commonFacade.bundle, "ALERT_ERROR_SAVE");
					break;
					
				case MintViewFacade.UPDATE_NOTICE_COMPLETE:
					stopWriting();
					break;
				case MintViewFacade.UPDATE_NOTICE_FAILED:
					CommonAlert.showMessage(commonFacade.bundle, "ALERT_ERROR_SAVE");
					break;
			}
		}		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	MVC Message handlers
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function startWriting():void
		{
			_mode = MODE_WRITING;
			setEventListener();
			view.dataProvider = null;
		}
		
		private function loadWriteData(vo:mintListVO):void
		{
 			var noticeWriteProxy:NoticeWriteProxy = facade.retrieveProxy(NoticeWriteProxy.NAME) as NoticeWriteProxy;
 			noticeWriteProxy.loadSearch(vo);
 			
		}
		
		private function loadNewWriteData(pvo:ProfileVO):void{
			
			var vo:NoticeVO = new NoticeVO();
 			
 			vo.ORGTX 			= pvo.ORGTX1;
 			vo.PERNR 			= pvo.PERNR;
 			vo.CDATE 			= new Date(DateUtil.todayNoTime());
 			vo.ENAME 			= pvo.ENAME;
 			vo.VIEWR 			= Variables.myPernr;
 			vo.VNAME 			= Variables.myName;
 			vo.JCTXT 			= Variables.myPosition;
 			vo.JINFO 			= pvo.JCTXT+"/"+pvo.CTASK;
			vo.NEWCHK			= "NEW";
			vo.MFLAG			= "X";
			view.fileListArr = new ArrayCollection();
			view.dataProvider = vo;
		}
		
		private function stopWriting():void
		{
			_mode = MODE_WAITING;
 			clearView();
 			Alert.show("저장되었습니다.");
 			facade.sendNotification(MintViewFacade.CLOSED_REQUEST_VIEW, true);
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function onCancel(e:Event):void
		{
			clearView();
 			bActivated = false;
			facade.sendNotification(MintViewFacade.CLOSED_REQUEST_VIEW, this);
		}
		
		
		private function buildSaveData():NoticeDetailVO
		{
			var vo:NoticeDetailVO = new NoticeDetailVO();
 			
 			vo.ORGTX 			= view.vbORGTX.text;
 			vo.DATUM			= view.Ibegda.selectedDate;
 			vo.PERNR 			= view.vbPERNR.text;
 			vo.CDATE 			= new Date(view.vbCDATE.text);
 			vo.ENAME 			= view.vbENAME.text;
 			vo.VNAME 			= view.vbVNAME.text;
 			vo.JINFO 			= view.vbJINFO.text;
 			vo.STIME 			= view.getBeginTime();
 			vo.ETIME 			= view.getEndTime();
 			vo.TITLE			= view.titleNm.text;
 			vo.CONTENT			= view.content.htmlText;
 			vo.CONTX			= view.content.text;
 			vo.SEQNR			= view._dataProvider.SEQNR;
 			vo.AEDTM			= view._dataProvider.AEDTM;
 			vo.CDATE			= view._dataProvider.CDATE;
 			vo.CTIME			= view._dataProvider.CTIME;
 			vo.MANDT			= view._dataProvider.MANDT;
 			vo.SRTFD			= view._dataProvider.SRTFD;
 			vo.UZEIT			= view._dataProvider.UZEIT;
 			vo.VNAME			= view._dataProvider.VNAME;
 			vo.VIEWR			= view._dataProvider.VIEWR;
 			
 			
// 			if(fileInfo!=null) {
//	 			vo.I_FILE_NAME		= fileInfo.I_FILE_NAME;
//	 			vo.I_FILE_EXT		= fileInfo.I_FILE_EXT;
//	 			vo.I_FILE_URL		= fileInfo.I_FILE_URL;
//	 			vo.I_FILE_SIZE		= fileInfo.I_FILE_SIZE.toString();
//	 		}
 			return vo;
		}
		
 		//private var vo:NoticeDetailVO = new NoticeDetailVO();
 		private function onNoticeSave(event:Event):void
 		{
 			saveNotice();
 		}
		
		private function saveNotice():void
		{
			_bWaitingSave = false;
			var proxy:NoticeWriteProxy = facade.retrieveProxy(NoticeWriteProxy.NAME) as NoticeWriteProxy;
			proxy.update(buildSaveData(),view.fileListArr); 				
		}
		
 		private function onNoticeDelete(event:Event):void
 		{
			var param:Object = new Object();
  			param.I_BOARD_ID 		= "01"
 			param.CONTENT_IDX		= view.writeKey;

 		}
 		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function validateFields():Boolean
		{
			if(view.titleNm.text == "") {
				CommonAlert.showMessage("Notice", "ALERT_NO_TITLE");
				return false;
			}
			if(view.content.text == "") {
				CommonAlert.showMessage("Notice", "ALERT_NO_CONTENT");
				return false;
			}
			return true;
		}
		
        private function get view():NoticeWrite
        {
            return viewComponent as NoticeWrite;
        }
	}
}